package dgut.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import dgut.bean.Employee;
import dgut.bean.Msg;
import dgut.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EmployeeController {


    @Autowired//byType
    EmpService empService;

    /*
    * 分页查询
    * */
    @RequestMapping("emps")
    @ResponseBody
    public Msg getEmps(@RequestParam(value="pn",defaultValue="1")Integer pn){

        System.out.println("请求成功");
        //传入页码，以及每页的大小
        PageHelper.startPage(pn,5);
        List<Employee> emps = empService.getAll();
        //使用pageInfo包装查询后的结果,连续显示5页
        PageInfo page = new PageInfo(emps,5);

        return Msg.success().add("pageInfo",page);
    }

    //新增的员工
    @RequestMapping(value = "emps" ,method = RequestMethod.POST)
    @ResponseBody
    public Msg saveEmps(@Valid Employee employee, BindingResult result){
        if(result.hasErrors()){
            Map<String,Object> map = new HashMap<>();
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError fieldError:errors){
                System.out.println("错误字段名：" + fieldError.getField());
                System.out.println("错误信息："+ fieldError.getDefaultMessage());
                map.put(fieldError.getField(),fieldError.getDefaultMessage());
            }
            return Msg.fail().add("errorFields",map);
        }
        empService.saveEmps(employee);
        return Msg.success();
    }

    //从员工id获取需要更改的信息
    @RequestMapping(value="empQueryById",method = RequestMethod.GET)
    @ResponseBody
    public Msg getEmp(@RequestParam("id")Integer id){
        Employee employee = empService.getEmp(id);
        return Msg.success().add("emp",employee);

    }

    //更新员工信息
    @RequestMapping(value = "empUpdate/{empId}",method=RequestMethod.PUT)
    @ResponseBody
    public Msg updateEmp(Employee employee, @PathVariable("empId")Integer empId){
        System.out.println("请求Id值" + empId);
        System.out.println("将要更新的员工数据" + employee);
        int result = empService.updateEmp(employee);
        if(result>0) {
            System.out.println("更新成功");
            return Msg.success();
        }
        System.out.println("更新失败");
        return Msg.fail();
    }

    //校验用户名是否重复
    @RequestMapping(value = "checkUser", method = RequestMethod.POST)
    @ResponseBody
    public Msg checkUser(@RequestParam("empName")String empName){

        boolean repeat = empService.checkUser(empName);
        if(repeat){
            return Msg.success();
        }else{
            return Msg.fail().add("va_msg","用户名已存在");
        }
    }

    //单个批量二合一删除
    /*
    * 批量删除1-2-3.。。
    * 单个删除：1
    * */
    @ResponseBody
    @RequestMapping(value = "empDelete/{ids}",method=RequestMethod.DELETE)
    public Msg deleteEmpById(@PathVariable("ids")String ids){

        int result ;
        if(ids.contains("-")){
            String[] split = ids.split("-");
            List<Integer> idList = new ArrayList<>();
            for(String id :split){
                idList.add(Integer.valueOf(id));
            }
            result = empService.deleteEmp(idList);
        }else{
            result = empService.deleteEmp(Integer.valueOf(ids));
        }

        if (result>0){
            return Msg.success();
        }
        return Msg.fail();

    }

}
